/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._404;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._404
 * @ClassName: SumOfLeft
 * @Author: linyaonan
 * @Date: 2019/12/26 15:29
 */
public class SumOfLeft {
    private Integer count = 0;

    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return 0;
        }
        bfsTree(root, true);
        return count;

    }

    private void bfsTree(TreeNode tree, boolean isLeft) {
        if (tree.left == null && tree.right == null && isLeft) {
            count +=tree.val;
        } else {
            if (tree.left != null) {
                bfsTree(tree.left, true);
            }
            if (tree.right != null) {
                bfsTree(tree.right, false);
            }
        }
    }
}
